package com.walkline.weibo.dao;

import java.util.Vector;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import com.walkline.util.Function;
import com.walkline.weibo.WeiboException;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.User;
import com.walkline.weibo.inf.UserWrapper;

public class WeiboUserWrapper extends WeiboObject implements UserWrapper
{
	private int _total_number = 0;
	private int _next_cursor = 0;
	private int _previous_cursor = 0;
	private Vector _users = new Vector();

	public WeiboUserWrapper(WeiboSDK weibo, JSONObject jsonObject) throws WeiboException
	{
		super(weibo, jsonObject);

		JSONObject userWrapper = jsonObject;
		if (userWrapper != null)
		{
			_total_number = userWrapper.optInt("total_number");
			_next_cursor = userWrapper.optInt("next_cursor");
			_previous_cursor = userWrapper.optInt("previous_cursor");

			JSONArray users = userWrapper.optJSONArray("users");
			if (users != null)
			{
				JSONObject userObject;
				User user;

				for (int i=0; i<users.length(); i++)
				{
					try {
						userObject = (JSONObject) users.get(i);

						user = new WeiboUser(weibo, userObject);
						if (user != null) {_users.addElement(user);}
					} catch (JSONException e) {Function.errorDialog(e.toString());}
				}
			}
		}
	}

	public int getTotalNumber() {return _total_number;}

	public String getNextCursor() {return String.valueOf(_next_cursor);}

	public String getPreviousCursor() {return String.valueOf(_previous_cursor);}

	public Vector getUserList() {return _users;}
}